package table

import (
	"crypto/sha256"
	"encoding/json"
	"fmt"
	"goProject/db"
	"time"
)

func init() {
	_ = db.MySQL.AutoMigrate(&User{})
	_ = db.MySQL.AutoMigrate(&Role{})
	_ = db.MySQL.AutoMigrate(&RegisterCode{})
	_ = db.MySQL.AutoMigrate(&Profile{})
	_ = db.MySQL.AutoMigrate(&RoleMenuItem{})

	db.MySQL.FirstOrCreate(&Role{ID: 1, Key: "root", Name: "ROOT"})
	db.MySQL.FirstOrCreate(&Role{ID: 2, Key: "admin", Name: "Admin"})
	_code := fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprint(time.Now()))))
	db.MySQL.FirstOrCreate(&RegisterCode{ID: 1, RoleID: 1, UserID: 0, Code: _code, RegisterAt: 0})
}

// User 账号
type User struct {
	ID        uint  `json:"id" gorm:"primarykey"` //账号ID
	CreatedAt int64 `json:"created_at"`           //注册时间

	Username string `json:"username" gorm:"type:varchar(20);index:unique;"` //用户名
	Password string `json:"-" gorm:"type:varchar(255)"`                     //登录密码，加密版

	Lock int `json:"lock" gorm:"type:tinyint(1);default:1;comment:是否锁定账号，1正常，2已锁定"`

	Profile *Profile `json:"profile" gorm:"foreignKey:id"`

	Vip       int    `json:"vip" gorm:"type:tinyint(1);default:1;"`        //VIP等级
	AdminName string `json:"admin_name,omitempty" gorm:"type:varchar(50)"` //管理员才能看到的备注名称
}

func (User) TableName() string {
	return "user_account"
}

func (u User) String() string {
	marshal, _ := json.Marshal(&u)
	return string(marshal)
}

type Role struct {
	ID   uint   `json:"id" gorm:"primarykey"`                      //角色ID
	Key  string `json:"key" gorm:"type:varchar(50);index:unique;"` //唯一标识符号
	Name string `json:"name,omitempty" gorm:"type:varchar(255);"`  //人性化名称
}

func (Role) TableName() string {
	return "user_role"
}

// RoleMenuItem 角色拥有的菜单项
type RoleMenuItem struct {
	RoleID     uint `json:"role_id" gorm:"index:role_menu,unique;"`
	MenuItemID uint `json:"menu_item_id" gorm:"index:role_menu,unique;"`
}

func (RoleMenuItem) TableName() string {
	return "user_role_menu_item"
}

// RegisterCode 注册码
type RegisterCode struct {
	ID        uint  `json:"id" gorm:"primarykey"` //账号ID
	CreatedAt int64 `json:"created_at"`           //生成时间

	RoleID     uint   `json:"role_id"` //注册完成后分配的角色ID
	Role       *Role  `json:"role"`
	UserID     uint   `json:"user_id"`                                     //注册完成后生成的用户ID
	Code       string `json:"code" gorm:"type:varchar(255);index:unique;"` //注册码
	RegisterAt int64  `json:"register_at"`                                 //注册时间
	CmdUserID  uint   `json:"cmd_user_id"`                                 //创建该注册码的用户
}

func (RegisterCode) TableName() string {
	return "user_register_code"
}

func (r RegisterCode) String() string {
	marshal, _ := json.Marshal(&r)
	return string(marshal)
}

type Profile struct {
	ID        uint  `json:"id" gorm:"primarykey"` //账号ID
	UpdatedAt int64 `json:"updated_at"`           //更新时间

	RoleID uint  `json:"role_id"` //注册完成后分配的角色ID
	Role   *Role `json:"role"`    //角色信息

	Coin float64 `json:"coin"` //金币数量
}

func (Profile) TableName() string {
	return "user_account_profile"
}

func (p Profile) String() string {
	marshal, _ := json.Marshal(&p)
	return string(marshal)
}
